﻿Public Class clsSymbolScanner

    Friend MyReader As Symbol.Barcode.Reader = Nothing
    Friend MyReaderData As Symbol.Barcode.ReaderData = Nothing
    Friend MyEventHandler As System.EventHandler = Nothing
    Public Event ScanComplete(ByVal bstrBarCode As String, ByVal bstrSource As String, ByVal lStatus As Integer, ByVal lLabelType As Integer, ByVal lDataLength As Integer)
    Friend bEnabled As Boolean = False
    Friend bSleep As Boolean

    Friend Function InitReader() As Boolean

        ' If reader is already present then fail initialize
        If Not (Me.MyReader Is Nothing) Then

            Return False

        End If

        'Create new reader, first available reader will be used.
        Me.MyReader = New Symbol.Barcode.Reader

        'Create reader data
        Me.MyReaderData = New Symbol.Barcode.ReaderData( _
                                     Symbol.Barcode.ReaderDataTypes.Text, _
                                     200)

        ' create event handler delegate
        Me.MyEventHandler = New System.EventHandler(AddressOf MyReader_ReadNotify)

        'Enable reader, with wait cursor
        Me.MyReader.Actions.Enable()

        '2/5ITF gen. An
        Me.MyReader.Decoders.I2OF5.MinimumLength = 4
        Me.MyReader.Decoders.I2OF5.MaximumLength = 20
        Me.MyReader.Decoders.I2OF5.Enabled = True

        'Me.MyReader.Parameters.Feedback.Success.BeepTime = 1 'Wenn der Eintrag da ist, Peept die Gemini nicht !

        'Me.MyReader.Parameters.Feedback.Success.WaveFile = "\\windows\\alarm3.wav"

        Return True

    End Function

    Friend Sub TermReader()

        'If we have a reader
        If Not (Me.MyReader Is Nothing) Then

            'Disable reader, with wait cursor
            Me.MyReader.Actions.Disable()

            'free it up
            Me.MyReader.Dispose()

            ' Indicate we no longer have one
            Me.MyReader = Nothing

        End If

        ' If we have a reader data
        If Not (Me.MyReaderData Is Nothing) Then

            'Free it up
            Me.MyReaderData.Dispose()

            'Indicate we no longer have one
            Me.MyReaderData = Nothing

        End If

    End Sub

    Friend Sub StartRead()

        'If we have both a reader and a reader data
        If Not ((Me.MyReader Is Nothing) And (Me.MyReaderData Is Nothing)) Then

            'Submit a read
            AddHandler MyReader.ReadNotify, Me.MyEventHandler

            Me.MyReader.Actions.Read(Me.MyReaderData)

        End If

    End Sub

    Friend Sub StopRead()

        'If we have a reader
        If Not (Me.MyReader Is Nothing) Then

            'Flush (Cancel all pending reads)
            Me.MyReader.Actions.Flush()

            RemoveHandler MyReader.ReadNotify, Me.MyEventHandler
        End If

    End Sub

    Friend Sub MyReader_ReadNotify(ByVal o As Object, ByVal e As EventArgs)

        Dim TheReaderData As Symbol.Barcode.ReaderData = Me.MyReader.GetNextReaderData()

        'If it is a successful read (as opposed to a failed one)
        If (TheReaderData.Result = Symbol.Results.SUCCESS) Then
            'Handle the data from this read
            Me.HandleData(TheReaderData)
        End If

        'Start the next read
        If Me.Enabled Then Me.MyReader.Actions.Read(Me.MyReaderData)

    End Sub

    Friend Sub HandleData(ByVal TheReaderData As Symbol.Barcode.ReaderData)
        Beep()
        RaiseEvent ScanComplete(TheReaderData.ToString, TheReaderData.Source, 1, TheReaderData.Type, Len(TheReaderData.ToString))
    End Sub

    Public Sub New()
        Me.InitReader()
    End Sub

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub

    Public Property Enabled() As Boolean
        Get
            Return Me.bEnabled
        End Get
        Set(ByVal Value As Boolean)
            If Value Then
                Me.StartRead()
                Me.bEnabled = True
            Else
                Me.StopRead()
                Me.bEnabled = False
            End If
        End Set

    End Property

    Public Sub Dispose()
        Me.TermReader()
    End Sub

End Class
